package com.cloudkinto.controller.channel;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.vo.channel.la.LaCustomerAddReq;
import com.cloudkinto.vo.channel.la.LaPageRes;
import com.cloudkinto.vo.channel.la.LaSwitchReq;
import com.cloudkinto.vo.channel.la.LaUpdateReq;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.channel.LogisticsAccountService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-10-29
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "物流商账号omp模块")
@RequestMapping("/api/admin/omp/LogisticsAccount")
public class LogisticsAccountController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(LogisticsAccountController.class);

    private final LogisticsAccountService service;

    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "新增(自定义物流商)", notes = "新增(自定义物流商)")
    public SingleResult addLa(@RequestBody @Validated LaCustomerAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功", service.addLa(req, userId, getTenantId()));
    }

    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated LaUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }


    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "授权状态", dataType = "arr"),
            @ApiImplicitParam(name = "enable", value = "状态"),
            @ApiImplicitParam(name = "name", value = "账户名称"),
            @ApiImplicitParam(name = "serviceName", value = "物流商名称")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = LaPageRes.class))
    public SingleResult list(HttpServletRequest request) {
        Map<String, Object> map = this.getMap(request);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "授权状态", dataType = "arr"),
            @ApiImplicitParam(name = "enable", value = "状态"),
            @ApiImplicitParam(name = "name", value = "账户名称"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "serviceName", value = "物流商名称")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = LaPageRes.class))
    public PageResult listPage(HttpServletRequest request) {

        Map<String, Object> map = this.getMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    public Map<String, Object> getMap(HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();
        String[] statuses = request.getParameterValues("status");
        if (statuses != null && statuses.length > 0) {
            map.put("status", Arrays.asList(statuses));
        }
        String enable = request.getParameter("enable");
        String name = request.getParameter("name");
        String serviceName = request.getParameter("serviceName");

        map.put("enable", enable);
        map.put("name", name);
        map.put("serviceName", serviceName);
        map.put("tenantId", getTenantId());

        return map;
    }

    @UserSessionRequired
    @PutMapping("/switch")
    @ApiOperation(value = "启用 禁用", notes = "启用")
    public SingleResult switchAccount(@RequestBody @Validated LaSwitchReq req) {
        Long userId = getCurrentUserId();
        service.switchAccount(req, userId);
        return SingleResult.success("修改成功", "");
    }

}

